package main

import (
	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go"
	"time"

	jaegercfg "github.com/uber/jaeger-client-go/config"
)

func SpanTest() {
	cfg := jaegercfg.Configuration{
		Sampler: &jaegercfg.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		Reporter: &jaegercfg.ReporterConfig{
			LogSpans:           true,
			LocalAgentHostPort: "192.168.56.101:6831",
		},
		ServiceName: "sale-system",
	}

	tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
	if err != nil {
		panic(err)
	}
	defer closer.Close()

	parentSpan := tracer.StartSpan("main")
	defer parentSpan.Finish()

	span1 := tracer.StartSpan("func1", opentracing.ChildOf(parentSpan.Context()))
	time.Sleep(time.Second)
	span1.Finish()

	span2 := tracer.StartSpan("func2", opentracing.ChildOf(parentSpan.Context()))
	time.Sleep(time.Second * 2)
	span2.Finish()
}

func main() {
	SpanTest()
}
